/*
 *
 *
 */
#include "gkt_dac.h"
#include "gkt_adc.h"
#include "gkt_battery.h"

#ifdef GKT_CONFIG_BATTERY_ADC_ADJUST_VALUE_MAX
#define BATTERY_ADC_ADJUST_VALUE_MAX	\
	GKT_CONFIG_BATTERY_ADC_ADJUST_VALUE_MAX
#else
#define BATTERY_ADC_ADJUST_VALUE_MAX	100
#endif

#ifdef GKT_CONFIG_BATTERY_ADC_ADJUST_VALUE_MIN
#define BATTERY_ADC_ADJUST_VALUE_MIN	\
	GKT_CONFIG_BATTERY_ADC_ADJUST_VALUE_MIN
#else
#define BATTERY_ADC_ADJUST_VALUE_MIN	-100
#endif

#define	BATTERY_R1_R2_VALUE	\
	(GKT_CONFIG_BATTERY_R2_VALUE + GKT_CONFIG_BATTERY_R1_VALUE)	//unit	k

#define	BATTERY_VOLTAGE_2_ADC_VALUE(voltage_mv)	(voltage_mv)

#define GKT_BATTERY_VOLTAGE(voltage_mv)	\
	BATTERY_VOLTAGE_2_ADC_VALUE(voltage_mv)
static const uint32_t sc_battery_adc_threshold_table[GKT_BATTERY_LEVEL_MAX] = {
	0, 
	GKT_CONFIG_BATTERY_VOLTAGE_TABLE
};

#define BATTERY_ABS(x) ((x) < 0 ? (-(x)) : (x))

typedef struct _battery_instance {
	int	adc_adjust_value;
} battery_instance_s;
static battery_instance_s s_battery_instance;

static uint32_t battery_get_adc_value(void)
{
	uint32_t adc_value = 0;
	uint8_t dev_id = 0;
	gkt_adc_attr_s adc_attr;
	int retval, value;

	adc_attr.channel = ADC_USED_CHANNEL;
	adc_attr.mode = ADC_GPIO;//ADC_VINLPM
	adc_attr.scale_type = ADC_Scale_1200Mv;
	retval = gkt_adc_config(dev_id, &adc_attr);
	if (retval != GKT_SUCCESS)
		gkt_error("adc config err=%d\n",retval);

	retval = gkt_adc_start(0, &value);
	if (retval == GKT_SUCCESS) 
		adc_value = (uint32_t)value;
	else
		gkt_error("battery: get adc value failed %d\n", retval);
		
	return adc_value;
}

int gkt_battery_init(void)
{
	return GKT_SUCCESS;
}

void gkt_battery_calibrate(int *adc_adjust_value)
{
#if 0
	uint32_t adc_value, adc_base_value;

	adc_value = battery_get_adc_value();
	__ISB();
	adc_value = battery_get_adc_value();

	adc_base_value = BATTERY_VOLTAGE_2_ADC_VALUE(GKT_CONFIG_BATTERY_CALIBRATE_VOLTAGE);
#endif
}

void gkt_battery_set_adc_adjust_value(int adc_adjust_value)
{
	gkt_info("battery_set: adc_adjust_value = %d\n", adc_adjust_value);

	//if ((adc_adjust_value >= BATTERY_ADC_ADJUST_VALUE_MIN)
	//	&& (adc_adjust_value <= BATTERY_ADC_ADJUST_VALUE_MAX))
	{
		s_battery_instance.adc_adjust_value = adc_adjust_value;
	}
}
gkt_battery_level_e gkt_battery_get_level(void)
{
	uint32_t adc_value, adc_base_value;
	gkt_battery_level_e level;

	adc_base_value = battery_get_adc_value();
	__ISB();
	adc_base_value = battery_get_adc_value();
	gkt_printf("get level:adc_value=%d adjust_value=%d\n",adc_base_value, s_battery_instance.adc_adjust_value);


	adc_value = adc_base_value * GKT_CONFIG_BATTERY_RESISTOR_VALUE / 1000 \
		+ GKT_CONFIG_BATTERY_VOLTAGE_DROP - s_battery_instance.adc_adjust_value;


	gkt_printf("get level: adc final value=%d mv\n", adc_value);
#if 0	
#if defined(GKT_CONFIG_BACKBOARD_ENABLE) && GKT_CONFIG_BACKBOARD_ENABLE
#if !defined(GKT_CONFIG_BATTERY_FACTORY_INSPECT_DEVELOPER_MODE)	|| !GKT_CONFIG_BATTERY_FACTORY_INSPECT_DEVELOPER_MODE
	if (adc_value <= 5800) {
		gkt_printf("Turn off sound using USB power supply\n");
		gkt_dac_play_switch(GKT_DAC_PLAY_SWITCH_CLOSE);
	}
#endif	
#endif
#endif
	level = GKT_BATTERY_LEVEL_25;
	do {
		gkt_info("adc_thre_table[%d]=%d\n", level, sc_battery_adc_threshold_table[level]);
		if (adc_value < sc_battery_adc_threshold_table[level]) {
			level--;
			break;
		}
		else{
			level++;
		}
	} while (level < GKT_BATTERY_LEVEL_MAX);
	gkt_info("adc level=%d\n",level);
	return level;
}

